home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11225 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  58 lines

  1. Path: ix.netcom.com!news
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Wierd const problem (repost)
  5. Date: Fri, 22 Mar 1996 17:21:37 GMT
  6. Organization: Netcom
  7. Message-ID: <3152d883.184774812@nntp.ix.netcom.com>
  8. References: <827481377.25066@redifon.demon.co.uk>
  9. NNTP-Posting-Host: ix-dc6-04.ix.netcom.com
  10. X-NETCOM-Date: Fri Mar 22 11:21:18 AM CST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Guy Pickering <gp@redifon.demon.co.uk> wrote:
  14.  
  15. > If I declare a function:
  16. >         void foo(const char* s)
  17. > This means 's' is a pointer to a constant char. I.e. I promise not
  18. > to modify 's' inside the function.
  19. > But, how do I declare a function where 's' is a pointer to a pointer
  20. > to a contstant char. The following doesn't work quite right:
  21. >         void foo(const char** s)
  22. > Because when I try to pass a 'char**' info the function, the compiler
  23. > complains about incompatible types. Is it that the const in 
  24. > 'const char**' doesn't refer to the 'char'? If so how can I declare
  25. > what I mean?
  26.  
  27. You're doing it right.  const char** s declares s as a pointer to
  28. pointer to const char.  However, the language does not allow automatic
  29. conversion from char** to const char** because it would introduce a
  30. security hole.  Consider:
  31.  
  32.     const char a[] = "abc";
  33.     const char* b = a;
  34.     char* d[] = { 0 };
  35.     char** e = d;
  36.     const char** f = e;    /* illegal */
  37.     f[0] = b;
  38.     e[0][0] = 'x';        /* changes a[0] without a cast */
  39.  
  40. If you must pass a char** where a const char** is expected, use a
  41. cast.
  42.  
  43. Note that the above security hole would not occur if f were declared
  44. as
  45.  
  46.     const char* const* f;
  47.  
  48. C++ allows conversion from char** to const char* const*, but (I assume
  49. due to an oversight) the C standard does not.
  50.  
  51.  
  52. Michael M Rubenstein
  53.